﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Collections.Specialized;
using Microsoft.Win32;
using System.Windows.Forms;

namespace TsChanManLibrary
{
    public struct Settings
    {
        /// <summary>
        /// Full Path and Filename to VideoLAN.exe
        /// </summary>
        public string sVlcExe;

        /// <summary>
        /// Full Path and Filename to TSReader.exe
        /// </summary>
        public string sTsReaderExe;

        /// <summary>
        /// Filename of the last recording
        /// </summary>
        public string sLastRecordingFile;

        /// <summary>
        /// Specifies if DVB-S High- and Lowband frequencies are set automatically
        /// </summary>
        public Boolean bAutoDVBs;

        public string sFavName1;
        public string sFavName2;
        public string sFavName3;
        public string sFavName4;
        public string sFavName5;

        /// <summary>
        /// Die zuletzt verwendete Favoritengruppe (1-5)
        /// </summary>
        public short lastFavorite;

        // Command-Line parameters for TSReader
        public Boolean bStartTSReaderMinimized;
        public Boolean bStartTSReaderMinimizedToNtfcArea;
        public Boolean bSingleTSReaderInstance;
        public Boolean bUniProcessorTSReader;
        public Boolean bDisableTSReaderThumbnailStreamParsing;
        // more...

        // TSReader Server settings
        public int iServerPort; // Default: 5934;
        public string sServerIP;
        public string sVlcServerCommand;

        public ChannelType cTvHardware;

        /// <summary>
        /// List of all the files converted from old file-format to the new one
        /// </summary>
        public StringCollection saConvertedFiles;

        public Boolean bReadServerWarning;

        /// <summary>
        /// Specifies if the VLC ActiveX plugin is used for client playback
        /// </summary>
        public Boolean bInternalVlcPlayer;
    }

    public struct RecordingSettings
    {
        public RecordingSettings(string RecordingFile, TimeSpan RecordingTime, 
                                bool SplitRecording, int SplitFileSize, bool TimedRecording,
                                DateTime TimedRecordingTime, bool AsProgramStream, 
                                bool IncludeSubtitles, bool EntireTS)
        {
            sRecordingFile = RecordingFile;
            tsRecordingTime = RecordingTime;
            bSplitRecording = SplitRecording;
            iSplitFileSize = SplitFileSize;
            bTimedRecording = TimedRecording;
            dtTimedRecordingTime = TimedRecordingTime;
            bAsProgramStream = AsProgramStream;
            bIncludeSubtitles = IncludeSubtitles;
            bEntireTS = EntireTS;
        }

        /// <summary>
        /// The file to which should be recorded to
        /// </summary>
        public string sRecordingFile;

        /// <summary>
        /// The amount of time which should be recorded
        /// </summary>
        public TimeSpan tsRecordingTime;

        public bool bSplitRecording;

        public int iSplitFileSize;

        public bool bTimedRecording;

        public DateTime dtTimedRecordingTime;

        public bool bAsProgramStream;

        public bool bIncludeSubtitles;

        public bool bEntireTS;
    }

    public class cSettings
    {
        /// <summary>
        /// The default command used by VLC to stream/transcode a channel
        /// </summary>
        public const string sVlcDefaultCommand = ":sout=#duplicate{dst=std{access=http,mux=ts,url={0}:7878}}";

        public const int iDefaultPort = 5934;

        // Directory used to store the Settings-File & Channel lists
        public static readonly string sAppDataDir = Path.Combine(Environment.GetFolderPath
                                                                (Environment.SpecialFolder.ApplicationData),
                                                                    @"TsChanMan\");

        private static readonly string sSettingFile = Path.Combine(sAppDataDir,
                                                                    "TsChanMan.Settings");

        public static Settings Cfg = new Settings();

        /// <summary>
        /// Checks if a configuration file exists on the hard disk
        /// </summary>
        public static bool CfgFileExists
        {
            get
            {
                if (File.Exists(sSettingFile))
                { return true; }
                else
                { return false; }
            }
        }

        /// <summary>
        /// Saves current Configuration to Disk
        /// </summary>
        public static void SaveCfg()
        {
            Directory.CreateDirectory(Path.GetDirectoryName(sSettingFile));

            cHelper.SerializeToXML(Cfg, sSettingFile);
        }

        /// <summary>
        /// Checks if the user agreed to overwrite VLC Configuration No. 16
        /// Also checks if VLC Configuration No. 16 is not yet used
        /// </summary>
        /// <returns></returns>
        public static bool CheckServerWarning()
        {
            if (Cfg.bReadServerWarning) { return true; }

            bool bOverWriteConfig = false;

            object VlcCommand15 = Registry.GetValue(@"HKEY_CURRENT_USER\Software\COOL.STF\TSReader", "VLCConfigCommand15", null);
            if (VlcCommand15 == null || VlcCommand15.GetType() != typeof(string) || string.IsNullOrEmpty(VlcCommand15.ToString()))
            { bOverWriteConfig = true; }
            else // VlcCommand15 is in use
            {
                if (MessageBox.Show("VLC Configuration number 15 is currently in use." + Environment.NewLine +
                                    "If you continue, TSReader Channel-Manager will overwrite the configuration." +
                                    Environment.NewLine + Environment.NewLine + "Continue?",
                                    "Overwrite current Configuration?",
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                { bOverWriteConfig = true; }
            }

            if (bOverWriteConfig)
            {
                // Set the description so the user knows this is configuration slot is
                // used by the TSReader Channel-Manager
                Registry.SetValue(@"HKEY_CURRENT_USER\Software\COOL.STF\TSReader", "VLCConfigDescription15", "TSReader Channel-Manager internal use");

                Cfg.bReadServerWarning = true; // So the user will not get the dialog next time
                SaveCfg();
                return true;
            }
            else
            { return false; }
        }

        /// <summary>
        /// Loads Configuration from Disk
        /// </summary>
        /// <returns>true if succesfull, false if "File not found"</returns>
        public static bool LoadCfg()
        {
            if (File.Exists(sSettingFile))
            {
                Cfg = (Settings)cHelper.DeserializeFromXML(typeof(Settings), sSettingFile);
                return true;
            }
            else
            { return false; }
        }
    }
}